﻿//
// Created by work on 24-6-5.
//

#include "CModelInfo.h"


std::map<CModelInfo::EModelCol, QString> CModelInfo::rtModelColToStringMap = {
    {CModelInfo::EModelCol::EName, "name"},
    {CModelInfo::EModelCol::EUser, "userid"},
    {CModelInfo::EModelCol::EDescr, "descr"},
    {CModelInfo::EModelCol::ESn, "sn"},
    {CModelInfo::EModelCol::EGroupIngs, "groupings"},
    {CModelInfo::EModelCol::ELocation, "location"},
    {CModelInfo::EModelCol::ECreate_time, "create_time"},
    {CModelInfo::EModelCol::EUpdate_time, "update_time"},
    {CModelInfo::EModelCol::EProducer, "producer"},
    {CModelInfo::EModelCol::EValid, "vaild"},
    {CModelInfo::EModelCol::EConfigs, "configs"}
};

std::map<CModelInfo::EModelCol, QString> CModelInfo::modelColFieldsNameMap = {
    {CModelInfo::EModelCol::EName, "模型名字"},
    {CModelInfo::EModelCol::EUser,"所属用户"},
    {CModelInfo::EModelCol::EDescr,"模型描述"},
    {CModelInfo::EModelCol::ESn,"排序"},
    {CModelInfo::EModelCol::EGroupIngs,"分组"},
    {CModelInfo::EModelCol::ELocation,"安装位置"},
    {CModelInfo::EModelCol::ECreate_time,"创建时间"},
    {CModelInfo::EModelCol::EUpdate_time,"更新时间"},
    {CModelInfo::EModelCol::EProducer,"生产厂商"},
    {CModelInfo::EModelCol::EValid,"是否有效"},
    {CModelInfo::EModelCol::EConfigs, "详细配置"}
};

std::map<CModelInfo::EModelCol, CValue::EValueType> CModelInfo::rtModelDataTypeByField = {
    {CModelInfo::EModelCol::EName, CValue::IOT_String},
    {CModelInfo::EModelCol::EUser, CValue::IOT_String},
    {CModelInfo::EModelCol::EDescr, CValue::IOT_String},
    {CModelInfo::EModelCol::ESn, CValue::IOT_Int},
    {CModelInfo::EModelCol::EGroupIngs, CValue::IOT_String},
    {CModelInfo::EModelCol::ELocation, CValue::IOT_String},
    {CModelInfo::EModelCol::ECreate_time, CValue::IOT_DateTime},
    {CModelInfo::EModelCol::EUpdate_time, CValue::IOT_DateTime},
    {CModelInfo::EModelCol::EProducer, CValue::IOT_String},
    {CModelInfo::EModelCol::EValid, CValue::IOT_Bool},
    {CModelInfo::EModelCol::EConfigs, CValue::IOT_Json}
};

CModelInfo::CModelInfo(QObject *parent) : IModel(MODEL_COL_SIZE, parent) {

}

QString CModelInfo::getFieldNameByCol(int col)
{
    if(col < 0 || col > MODEL_COL_SIZE)
        return "";
    return modelColFieldName(static_cast<CModelInfo::EModelCol>(col));
}

QVariant CModelInfo::getFieldValueByFielddName(const QString &name) {
    if(modelColFieldName(CModelInfo::EModelCol::EName) == name) {
        return this->name;
    } else if(modelColFieldName(CModelInfo::EModelCol::EUser) == name) {
        return user;
    } else if(modelColFieldName(CModelInfo::EModelCol::EDescr) == name) {
        return descr;
    } else if(modelColFieldName(CModelInfo::EModelCol::ESn) == name) {
        return sn;
    } else if(modelColFieldName(CModelInfo::EModelCol::EGroupIngs) == name) {
        return grouping;
    } else if(modelColFieldName(CModelInfo::EModelCol::ELocation) == name) {
        return location;
    } else if(modelColFieldName(CModelInfo::EModelCol::ECreate_time) == name) {
        return create_time.toString("yyyy-MM-dd hh:mm:ss");
    } else if(modelColFieldName(CModelInfo::EModelCol::EUpdate_time) == name) {
        return update_time.toString("yyyy-MM-dd hh:mm:ss");
    } else if(modelColFieldName(CModelInfo::EModelCol::EProducer) == name) {
        return producer;
    } else if(modelColFieldName(CModelInfo::EModelCol::EValid) == name) {
        return valid;
    } else if(modelColFieldName(CModelInfo::EModelCol::EConfigs) == name) {
        return QString(QJsonDocument(configs).toJson());
    }
    return QVariant();
}

void CModelInfo::setFieldValueByCol(int col, const QVariant &value) {

}

CValue CModelInfo::getValueByField(CModelInfo::EModelCol field) {
    switch (field) {
    case EModelCol::EName:
        return CValue(name);
        break;
    case EModelCol::EUser:
        return CValue(user);
        break;
    case EModelCol::EDescr:
        return CValue(descr);
        break;
    case EModelCol::ESn:
        return CValue(sn);
        break;
    case EModelCol::EGroupIngs:
        return CValue(grouping);
        break;
    case EModelCol::ELocation:
        return CValue(location);
        break;
    case EModelCol::ECreate_time:
        return CValue(create_time);
        break;
    case EModelCol::EUpdate_time:
        return CValue(update_time);
        break;
    case EModelCol::EProducer:
        return CValue(producer);
        break;
    case EModelCol::EValid:
        return CValue(valid);
        break;
    case EModelCol::EConfigs:
        return CValue(configs);
        break;
    default:
        break;
    }
    return CValue();
}

QJsonObject CModelInfo::toJson() {
    QJsonObject resmodel;
    resmodel[mdoelFieldName(MODEL_ENUM::EName)] = name;
    resmodel[mdoelFieldName(MODEL_ENUM::EUser)] = user;
    resmodel[mdoelFieldName(MODEL_ENUM::EDescr)] = descr;
    resmodel[mdoelFieldName(MODEL_ENUM::ESn)] = sn;
    resmodel[mdoelFieldName(MODEL_ENUM::ECreate_time)] = create_time.toString();
    resmodel[mdoelFieldName(MODEL_ENUM::EUpdate_time)] = update_time.toString();
    resmodel[mdoelFieldName(MODEL_ENUM::EGroupIngs)] = grouping;
    resmodel[mdoelFieldName(MODEL_ENUM::ELocation)] = location;
    resmodel[mdoelFieldName(MODEL_ENUM::EProducer)] = producer;
    resmodel[mdoelFieldName(MODEL_ENUM::EValid)] = valid;
    resmodel[mdoelFieldName(MODEL_ENUM::EConfigs)] = configs;
    return resmodel;
}

void CModelInfo::fromJson(const QJsonObject& json) {
    name = json[mdoelFieldName(MODEL_ENUM::EName)].toString();
    user = json[mdoelFieldName(MODEL_ENUM::EUser)].toString();
    descr = json[mdoelFieldName(MODEL_ENUM::EDescr)].toString();
    sn = json[mdoelFieldName(MODEL_ENUM::ESn)].toInt();
    grouping = json[mdoelFieldName(MODEL_ENUM::EGroupIngs)].toString();
    location = json[mdoelFieldName(MODEL_ENUM::ELocation)].toString();
    create_time = QDateTime::fromString(json[mdoelFieldName(MODEL_ENUM::ECreate_time)].toString(), "yyyy-MM-dd hh:mm:ss");
    update_time = QDateTime::fromString(json[mdoelFieldName(MODEL_ENUM::EUpdate_time)].toString(), "yyyy-MM-dd hh:mm:ss");
    producer = json[mdoelFieldName(MODEL_ENUM::EProducer)].toString();
    valid = json[mdoelFieldName(MODEL_ENUM::EValid)].toBool();
    configs = json[mdoelFieldName(MODEL_ENUM::EConfigs)].toObject();
}

QSharedPointer<IModel> CModelInfo::createEmpty()
{

}

bool CModelInfo::sync(bool dir)
{

}
